import {getContainerSources, getStorageSources, WORKSTATE, workSwitch} from "./public/CreepPublicAction.js";

/**
 * 维修者工作逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
export const repairerRun = (creep,sourcesIndex) => {
    //工作逻辑转换
    workSwitch(creep, RESOURCE_ENERGY)
    if (creep.memory[WORKSTATE]) {
        workCore(creep)
    } else {
        getSources(creep,sourcesIndex)
    }

};

const workCore = (creep) => {
    // 获取所有需要维护的路
    const structuresToMaintain = creep.room.find(FIND_STRUCTURES, {
        filter: (structure) => {
            return (structure.structureType === STRUCTURE_ROAD && structure.hits <= structure.hitsMax / 2)
                || (structure.structureType === STRUCTURE_CONTAINER && structure.hits <= structure.hitsMax / 2)
                || (structure.structureType === STRUCTURE_WALL && structure.hits <= 750000)
                || (structure.structureType === STRUCTURE_RAMPART && structure.hits <= 750000);
        }
    });
    // 如果有需要维护的建筑
    if (structuresToMaintain.length > 0) {
        // 找到最近的需要维护的建筑
        const closestStructure = creep.pos.findClosestByRange(structuresToMaintain);
        // 将creep移动到需要维护的建筑旁边并进行维护
        if (creep.repair(closestStructure) === ERR_NOT_IN_RANGE) {
            creep.moveTo(closestStructure);
        }
    } else {
        for (const roomsKey in Game.rooms) {
            creep.moveTo(33, 14)
        }
    }
}

const getSources = (creep,sourcesIndex) => {

    // getStorageSources(creep, 1)
    // const ruin_storage=Game.getObjectById("6624522153d8550012df1518")
    // //临时从废墟取能量
    // if (creep.withdraw(ruin_storage,RESOURCE_ENERGY)===ERR_NOT_IN_RANGE){
    //     creep.moveTo(ruin_storage)
    // }
    if (!getStorageSources(creep, sourcesIndex, "#aeef06")) {
        getContainerSources(creep, sourcesIndex, "#aeef06", 1000, true)
    }
}